package com.yupi.springbootinit.datasource;

import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.entity.Video;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.model.vo.UserVO;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataSourceRegister {

    @Resource
    private DataSource<Picture> pictureDataSource;

    @Resource
    private DataSource<PostVO> postVODataSource;

    @Resource
    private DataSource<UserVO> userVODataSource;

    @Resource
    private DataSource<Video> videoDataSource;

    private  Map<String,DataSource<?>> typeDataSourceMap;

    private DataSourceRegister(){}

    @PostConstruct
    public void doInit(){
        typeDataSourceMap = new HashMap(){{
            put(SearchTypeEnum.POST.getValue(),postVODataSource);
            put(SearchTypeEnum.PICTURE.getValue(),pictureDataSource);
            put(SearchTypeEnum.USER.getValue(),userVODataSource);
            put(SearchTypeEnum.VIDEO.getValue(),videoDataSource);
        }};
    }

    public DataSource<?> getDataSource(String type){
        if(typeDataSourceMap.isEmpty()){
            return null;
        }else{
            return typeDataSourceMap.get(type);
        }
    }

}
